package com.leyou.item.controller;

import com.leyou.item.entity.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**Request URL: http://api.leyou.com/api/item/category/list?pid=0

 * @author linzx
 * @date 2020/3/25
 */

@RestController
@RequestMapping("category")
public class CateGoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("list")
    public ResponseEntity<List<Category>> findCategoryByPid(@RequestParam("pid") Long pid) {
        return ResponseEntity.ok(categoryService.findCategoryByPid(pid));
    }

    @DeleteMapping("{id}")
    public ResponseEntity<Void> delectCategoryByid(@PathVariable("id") Long id) {
        categoryService.deleteByPrimaryKey(id);
        return ResponseEntity.noContent().build();
    }
    @PostMapping
    public ResponseEntity<Void> saveCategory(@RequestBody Category category) {
        categoryService.insertSelective(category);
        return ResponseEntity.noContent().build();
    }

    @PutMapping
    public ResponseEntity<Void> updateCategory(@RequestBody Category category) {
        categoryService.updateByPrimaryKeySelective(category);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryCategoryByBrandId(@PathVariable("bid")Long bid) {
        return ResponseEntity.ok(categoryService.findCategoryByBid(bid));
    }

    /**
     * 根据ids查询分类名称信息在搜索时用
     * @param ids
     * @return
     */
    @GetMapping("names")
    public ResponseEntity<List<String>> queryNamesByIds(@RequestParam("ids")List<Long> ids){
        List<String> names = categoryService.selectByIdList(ids).stream().map(Category::getName).collect(Collectors.toList());
        return ResponseEntity.ok(names);
    }

    @GetMapping("ids")
    public ResponseEntity<List<Category>> queryCategoryByIds(@RequestParam("ids")List<Long> ids){
        List<Category> names = categoryService.selectByIdList(ids);
        return ResponseEntity.ok(names);
    }



}
